package com.maochang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maochang.mapper.DengJiQuRen;
import com.maochang.mapper.PsInspectiontaskDetailMapper;
import com.maochang.mapper.PsInspectiontaskMainMapper;
import com.maochang.mapper.PsSysTemConfigMapper;
import com.maochang.pojo.PsInspectiontaskDetail;
import com.maochang.pojo.PsInspectiontaskMain;
import com.maochang.pojo.PsSystemconfig;
import com.maochang.pojo.PsUser;
import com.maochang.service.QueXianDengJiQuRenService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
@Service("QueXianDengJiQuRenImpl")
public class QueXianDengJiQuRenImpl extends ServiceImpl<PsInspectiontaskMainMapper, PsInspectiontaskMain> implements QueXianDengJiQuRenService {
    @Resource
    private PsInspectiontaskDetailMapper psInspectiontaskDetailMapper;
    @Resource
    private DengJiQuRen dengJiQuRen;
    @Resource
    private   PsInspectiontaskMainMapper mainMapper;
    @Resource
    private PsSysTemConfigMapper psSysTemConfigMapper;


    @Override
    public  Page<PsInspectiontaskDetail> CHaXunZiRenWu(PsInspectiontaskDetail psInspectiontaskDetail, int page, int size,String kai,String jie,HttpSession session) {
        //得到当前用户
        PsUser yongHu = (PsUser) session.getAttribute("loginUser");
        //查询  当前 用户  已完成 的任务id
        QueryWrapper<PsInspectiontaskMain> wrapper1= Wrappers.query();
        wrapper1.eq(!StringUtils.isBlank(yongHu.getUserName()),"issuedByName",yongHu.getUserName()).eq("taskStatusName","已完成").select("id");
        List<PsInspectiontaskMain> aa= mainMapper.selectList(wrapper1);

        //子表数据
        Page<PsInspectiontaskDetail> psPIPage=new Page<>(page,size);
        QueryWrapper<PsInspectiontaskDetail> wrapper= Wrappers.query();
        if(psInspectiontaskDetail.getBugLevel() != null){
            wrapper.eq("bugLevel",psInspectiontaskDetail.getBugLevel());
        }
        if(psInspectiontaskDetail.getBugType()!=null){
            wrapper.eq("bugType",psInspectiontaskDetail.getBugType());
        }
        wrapper.eq(!StringUtils.isBlank(psInspectiontaskDetail.getLineName()),"taskId",psInspectiontaskDetail.getTaskId())
                .eq(!StringUtils.isBlank(psInspectiontaskDetail.getLineCode()),"lineCode",psInspectiontaskDetail.getLineCode())
                 .eq(!StringUtils.isBlank(psInspectiontaskDetail.getPoleCode()),"poleCode",psInspectiontaskDetail.getPoleCode())
        .eq(!StringUtils.isBlank(psInspectiontaskDetail.getDiscovererName()),"discovererName",psInspectiontaskDetail.getDiscovererName())
         .ge(!StringUtils.isBlank(kai),"InspectTime",kai)
         .le(!StringUtils.isBlank(jie),"InspectTime",jie).eq("isBugLevel",0);
        if(aa.size()>0) {
            List<Integer> cc=new ArrayList<>();
            for (PsInspectiontaskMain a: aa) {
                cc.add(a.getId());
            }
            wrapper.in("taskId",cc);
        }else{
             wrapper.in("taskId","null");
        }
        Page<PsInspectiontaskDetail> psInspectiontaskDetailPage =dengJiQuRen.AllPXjList2(psPIPage,wrapper);
        return  psInspectiontaskDetailPage;
    }
    @Override
    public Boolean gaiZhuangTai(String id,String djid) {
        System.out.println(id);
        PsInspectiontaskDetail aa=new PsInspectiontaskDetail();
        //编号赋值
        aa.setBugLevel(Integer.valueOf(djid));
        //name 查询赋值
        QueryWrapper<PsSystemconfig> wrapper1= Wrappers.query();
        wrapper1.eq("id",djid);
        PsSystemconfig psSystemconfig = psSysTemConfigMapper.selectOne(wrapper1);
        aa.setBugLevelName(psSystemconfig.getConfigName());
        //是否确认缺陷级别
        aa.setIsBugLevel(1);
        //更改条件
        QueryWrapper<PsInspectiontaskDetail> wrapper2= Wrappers.query();
        wrapper2.eq("id",id);
        int update = psInspectiontaskDetailMapper.update(aa,wrapper2);
        return update>0?true:false;
    }

}
